#!/usr/bin/env python

import os
import sys
import textwrap
import xml.dom.minidom

outfile = sys.argv[1]

svn = os.popen('svn log https://python-slang.googlecode.com/svn/ --xml', 'r')
try:
    doc = xml.dom.minidom.parse(svn)
finally:
    svn.close()
entries = doc.getElementsByTagName('logentry')

lines = []

wrapper = textwrap.TextWrapper()
wrapper.initial_indent = '\t* '
wrapper.subsequent_indent = '\t'

for logentry in entries:
    date = logentry.getElementsByTagName('date')[0].childNodes[0].data
    date = date.split('T')[0]
    revision = logentry.getAttribute('revision')

    lines.append('%s  Chris Stawarz  <chris@pseudogreen.org>  r%s\n\n' %
                 (date, revision))

    msg = logentry.getElementsByTagName('msg')[0].childNodes[0].data.strip()

    if not msg.startswith('- '):
        msglist = [msg]
    else:
        msglist = msg[2:].split('\n- ')

    for msg in msglist:
        lines.append(wrapper.fill(msg.replace('\n ', '')) + '\n\n')


outfile = file(outfile, 'w')

try:
    outfile.writelines(lines)
finally:
    outfile.close()
